package com.tushare.controller;


import com.tushare.entity.DailyBasic;
import com.tushare.entity.FinaIndicator;
import com.tushare.entity.IndexMean;
import com.tushare.entity.StockBasic;
import com.tushare.service.IDailyBasicService;
import com.tushare.service.IFinaIndicatorService;
import com.tushare.service.IIndexMeanService;
import com.tushare.service.IStockBasicService;
import com.tushare.util.SortUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-14
 */
@RestController
@CrossOrigin("*")
public class IndexMeanController {
    @Autowired
    IIndexMeanService indexMeanService;
    @Autowired
    IDailyBasicService dailyBasicService;
    @Autowired
    IStockBasicService stockBasicService;
    @Autowired
    IFinaIndicatorService finaIndicatorService;
    @Resource(name = "testFxbDrawExecutor")
    ExecutorService asyncService;
    private final AtomicInteger atomicInteger = new AtomicInteger(0);

    @GetMapping("calculate_index_mean")
    public String claIndexMean() {
        if (atomicInteger.get() != 0) {
            return "正在进行处理数据，请稍等";
        }
        List<StockBasic> stockBasicList = stockBasicService.getAllStockBasic();
        stockBasicList.forEach(stockBasic -> {
            String ts_code = stockBasic.getTs_code();
            asyncService.submit(() -> {
                atomicInteger.incrementAndGet();
                if (indexMeanService.getIndexMeanCountByTsCode(ts_code).size() > 0) {
                    if (atomicInteger.get() == stockBasicList.size()) {
                        atomicInteger.set(0);
                    }
                    return;
                }
                List<DailyBasic> dailyBasicList = dailyBasicService.getStockBasicByTsCode(ts_code);
                if (dailyBasicList == null || dailyBasicList.size() == 0) {
                    if (atomicInteger.get() == stockBasicList.size()) {
                        atomicInteger.set(0);
                    }
                    return;
                }
                List<IndexMean> allIndexMeanList = new ArrayList<>();
                // pb
                allIndexMeanList.addAll(SortUtil.sortIndexMean(ts_code, "pb",
                        dailyBasicList.stream().map(DailyBasic::getPb)
                                .sorted(SortUtil::compareFloat)
                                .collect(Collectors.toList())));
                // pe
                allIndexMeanList.addAll(SortUtil.sortIndexMean(ts_code, "pe",
                        dailyBasicList.stream().map(DailyBasic::getPe)
                                .sorted(SortUtil::compareFloat)
                                .collect(Collectors.toList())));
                // ps
                allIndexMeanList.addAll(SortUtil.sortIndexMean(ts_code, "ps",
                        dailyBasicList.stream().map(DailyBasic::getPs)
                                .sorted(SortUtil::compareFloat)
                                .collect(Collectors.toList())));
                // turnover_rate
                allIndexMeanList.addAll(SortUtil.sortIndexMean(ts_code, "turnover_rate",
                        dailyBasicList.stream().map(DailyBasic::getTurnover_rate)
                                .sorted(SortUtil::compareFloat)
                                .collect(Collectors.toList())));
                // turnover_rate_f
                allIndexMeanList.addAll(SortUtil.sortIndexMean(ts_code, "turnover_rate_f",
                        dailyBasicList.stream().map(DailyBasic::getTurnover_rate_f)
                                .sorted(SortUtil::compareFloat)
                                .collect(Collectors.toList())));
                List<FinaIndicator> finaIndicatorList = finaIndicatorService.getFinaIndicatorByTsCode(ts_code);
                if (finaIndicatorList == null || finaIndicatorList.size() == 0) {
                    indexMeanService.saveIndexMeans(allIndexMeanList);
                    if (atomicInteger.get() == stockBasicList.size()) {
                        atomicInteger.set(0);
                    }
                    return;
                }
                // roa
                allIndexMeanList.addAll(SortUtil.sortIndexMean(ts_code, "roa",
                        finaIndicatorList.stream().map(FinaIndicator::getRoa)
                                .sorted(SortUtil::compareFloat)
                                .collect(Collectors.toList())));
                // roe
                allIndexMeanList.addAll(SortUtil.sortIndexMean(ts_code, "roe",
                        finaIndicatorList.stream().map(FinaIndicator::getRoe)
                                .sorted(SortUtil::compareFloat)
                                .collect(Collectors.toList())));
                indexMeanService.saveIndexMeans(allIndexMeanList);
                if (atomicInteger.get() == stockBasicList.size()) {
                    atomicInteger.set(0);
                }
            });
        });
        return "";
    }

}
